<?php
namespace app\admin\controller;
use app\admin\model\User;
use think\Request;
use think\Controller;
use think\Db;
use app\admin\model\Goodsimages;
use app\admin\model\Goods;
use app\admin\model\Attr;


  class goods extends Controller
  {

    //查询遍历商品详细信息
  	public function goods_list()
  	{
      //查询表attr获取手机的具体颜色 制式 价格 库存等属性属
      $list = Attr::where('display','in', '0,1')->order('create_time desc' )->paginate(4);
      //$list = Db::name('attr')->order('create_time desc' )->paginate(4);
      //var_dump($list[0]['create_time']);
      //exit();
      $this->assign("list",$list);
    
       $total = Db::name('attr')->select();
       $total = count($total);
      $this->assign("total",$total);
      //查询表goods获取手机的具体品牌 型号等属性
      $brand = Db::name('goods')->where('isalive',1)->select();
      $this->assign("brand",$brand);
      //var_dump($brand);
     // exit();

    //查询表goodsimages某款手机的图片
      $pricture = Db::name('Goodsimages')->where('image_id',1)->select();
      $this->assign("pricture",$pricture);
      //var_dump($pricture);
      //exit();
  		return $this->fetch();	
  	}

    //删除Attr表中具体一款手机
    public function list_delete()
    { 
      $request = Request::instance();
        if($request->isAjax()){ 
            $gid = input('mid');
          Attr::where('id',$gid)->delete(); 
      } 

    }
   
   //下架Attr表中的某一款手机
    public function shop_out()
    {
       $request = Request::instance();
        if($request->isAjax()){ 
            $gid = input('mid');
           //  Attr::where('id',$gid)->delete(); 
            /*$update = new Attr;
            $update->save([
                        'display'    =>  0,      
                    ],['id' => $git]);          
      } */
    //Attr::where('id',$gid)->update('display',0);  
     Db::execute("update ecmall_attr set display = 0 where id = $gid ");
      }
    }
  //修改商品的属性
  public function  goods_update(){

     //var_dump($_POST);
     //exit();
       $update = new Attr;
       $update->save([
                        'color'    =>  input('post.color'),    //颜色
                        'memory'   =>  input('post.memory'),    //内存
                        'version'  =>  input('post.version'),  //制式
                        'price'    =>  input('post.price'),    //价格
                        'stock'    =>  input('post.stock'),    //库存
                        'display'  =>  input('post.display'),  //商品上架
                    ],['id' => input('post.id')]);         

      $this->redirect('goods_list');
    }

  //产品发布页面展示
  	public function goods_edit()
  	{
    
     $brand = Db::name('goods')->where('isalive',1)->select();
     $this->assign("brand",$brand);
     return $this->fetch();
  		
  	}
   //新产品发布 增加
    public function goods_add()
    {
      
     $brand = $_POST['brand'];
     $class_id = intval($brand);
     $date = $_SERVER['REQUEST_TIME_FLOAT'];
     $add_attr = new Attr;
     $add_attr->save([
                        'goods_id' =>  "$class_id",             //具体某款手机的ID
                        'color'    =>  input('post.color'),    //颜色
                        'memory'   =>  input('post.memory'),    //内存
                        'version'  =>  input('post.version'),  //制式
                        'price'    =>  input('post.price'),    //价格
                        'stock'    =>  input('post.stock'),    //库存
                        'create_time' => "$date",
                    ]);  
     //查询最新增加品牌得一条数据
     $uid = Db::name('attr')->order('create_time desc')->find();
    //获取最新增加得id
     $id = $uid['goods_id'];                

     $file = request()->file('image');
     $info = $file->validate(['ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'images' . DS . 'goods');
        if($info){
            // 成功上传后 获取上传信息
            // echo $info->getExtension();
            //echo $info->getSaveName();
            // echo $info->getFilename();
            $saveName = $info->getSaveName();
            //$this->success('文件上传成功：' . $info->getRealPath());
            //var_dump($saveName);
            //exit();
           /* $result = Goodsimages::where('id',$id);*/
            $result = Goodsimages::where('id',$id)
                        ->update([
                                  'image1' => '/images/goods/' . $saveName,
                                  
                                   ]);
            } else {
            // 上传失败获取错误信息
            echo $file->getError();
        }
     $this->redirect('goods_edit');
    }


   //产品分类页面遍历数据展示
    public function goods_cate()
    {
     $count= Attr::where('display','in','0,1')->select();
     $this->assign("count",$count);
    /* var_dump(count($count));
     exit();*/

     $list = Goods::where('isalive','in','0,1')->order('reg_time desc')->paginate(4);
     $this->assign("list",$list);
     return $this->fetch();   
    }
    //下架该型号商品
     public function creat_out()
    {
       $request = Request::instance();
        if($request->isAjax()){ 
            $gid = input('mid');
           //  Attr::where('id',$gid)->delete(); 
            /*$update = new Attr;
            $update->save([
                        'display'    =>  0,      
                    ],['id' => $git]);          
      } */
    //Attr::where('id',$gid)->update('display',0);  
     Db::execute("update ecmall_goods set isalive = 0 where id = $gid ");
      }
    }
    //上架该型号商品
     public function creat_down()
    {
       $request = Request::instance();
        if($request->isAjax()){ 
            $gid = input('mid');
           //  Attr::where('id',$gid)->delete(); 
            /*$update = new Attr;
            $update->save([
                        'display'    =>  0,      
                    ],['id' => $git]);          
      } */
    //Attr::where('id',$gid)->update('display',0);  
     Db::execute("update ecmall_goods set isalive = 1 where id = $gid ");
      }
    }




    //产品分类描述及关键字修改展示  可用来做活动时修改内容打折宣传
    public function  goods_keys(){
       $update = new Goods;
       $update->save([
                        'abstract'    =>  input('post.abstract'),    //描述
                        'keywords'   =>  input('post.keywords'),    //关键字
                    ],['id' => input('post.id')]);         

      $this->redirect('goods_cate');
    }


    //新增手机品牌页面展示
    public function goods_append()
    {
    
      return $this->fetch();
    }
    //新增手机品牌
     public function goods_brand()
    {
  
     $date = $_SERVER['REQUEST_TIME_FLOAT'];
     $add_goods = new Goods;
     $add_goods->save([
                        
                        'brand'      =>  input('post.brand'),       //品牌
                        'category'   =>  input('post.category'),    //型号
                        'abstract'   =>  input('post.abstract'),    //描述
                        'keywords'   =>  input('post.keywords'),    //关键字
                        'reg_time'   =>  "$date",                   //新增时间
                    ]);
    //查询最新增加品牌得一条数据
     $uid = Db::name('goods')->order('reg_time desc')->find();
    //获取最新增加得id
     $id = $uid['id'];                

     $file = request()->file('image');
     $info = $file->validate(['ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'images' . DS . 'goods');
        if($info){
            // 成功上传后 获取上传信息
            // echo $info->getExtension();
            //echo $info->getSaveName();
            // echo $info->getFilename();
            $saveName = $info->getSaveName();
            //$this->success('文件上传成功：' . $info->getRealPath());
            //var_dump($saveName);
            //exit();
           /* $result = Goodsimages::where('id',$id);*/
            $result = new Goodsimages;
            $result->save([
                            'goods_id'  =>  "$id",
                            'main'      => '/images/goods/' . $saveName,
                             
                         ]);
            } else {
            // 上传失败获取错误信息
            echo $file->getError();
        }


     $this->redirect('goods_append');
    }

  



  }